//
//  YMClient.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/4/27.
//

import UIKit
import HandyJSON

class YMClient :HandyJSON,ObservableObject{
    @Published var night :Bool = false
    required init() {}
}
extension YMClient{
    static var client: YMClient = {
        let data = UserDefaults.standard.object(forKey:moya.defaultClient)
        let json = JSON(data as Any)
        guard let info = YMClient.deserialize(from: json.rawString()) else { return YMClient() }
        return info
    }()
    static func setNight(_ night :Bool){
        if moya.client.night != night{
            moya.client.night = night
            saveClient(moya.client)
        }
    }
    //MARK:保存信息
    static func saveClient(_ client :YMClient){
        moya.client = client
        
        let defaults = UserDefaults.standard
        
        guard let dic = client.toJSON() else { return }
        defaults.set(dic, forKey: moya.defaultClient)
        defaults.synchronize()
    }
    //MARK:删除信息
    static func deleteClient(){
        let defaults = UserDefaults.standard
        defaults.removeObject(forKey: moya.defaultClient)
        defaults.synchronize()
        moya.client = YMClient()
    }
}
